package com.njitzx.service.impl;

import com.njitzx.entity.po.LoginLog;
import com.njitzx.mapper.LoginLogMapper;
import com.njitzx.service.LoginLogService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.io.Serializable;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 系统登录日志 服务实现类
 * </p>
 *
 * @author 周鑫
 * @since 2024-08-20
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class LoginLogServiceImpl implements LoginLogService {

    private final LoginLogMapper loginLogMapper;

    /**
     * 系统登录日志分页查询
     * @param pageNum
     * @param pageSize
     * @param loginLog
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<LoginLog> findPage(Integer pageNum, Integer pageSize, LoginLog loginLog) {
        PageHelper.startPage(pageNum,pageSize);
        List<LoginLog> loginLogList = loginLogMapper.select(loginLog);
        return new PageInfo<>(loginLogList);
    }

    /**
     * 根据主键查询系统登录日志
     * @param infoId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public LoginLog findById(Serializable infoId) {
        return loginLogMapper.selectById(infoId);
    }

    /**
     * 系统登录日志新增
     * @param loginLog
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Async
    @Override
    public void add(LoginLog loginLog) {
        System.out.println("执行插入");
        loginLogMapper.insert(loginLog);
    }

    /**
     * 修改系统登录日志
     * @param loginLog
     */
    @Override
    public void modify(LoginLog loginLog) {
        loginLogMapper.update(loginLog);
    }

    /**
     * 根据主键删除系统登录日志
     * @param infoId
     */
    @Override
    public void removeById(Serializable infoId) {
        if(((String)infoId).contains(",")){
            loginLogMapper.deleteByIds(((String)infoId).split(","));
        }else {
            loginLogMapper.deleteById(infoId);
        }
    }
}
